var app = angular.module("editPApp", []),
	articleId = UI.getParameterByName('articleId'),
	ifmId = UI.getParameterByName('ifmId'),
	groupIdS = UI.getParameterByName('groupIdS'),
	ifmIdS = UI.getParameterByName('ifmIdS'),
	id = UI.getParameterByName('id');
	parid = UI.getParameterByName('parId');
	iTime = UI.getParameterByName('ifmTime');
var timeout_json=function(data,$window){ 
	if (data.returnCode == 999994) {
		var ss = 3;
		layer.msg(data.returnMessage + "！<strong class='Ajax_Out'>" + ss + "</strong> 秒后前往登录页", 50, -1);
		var picInt = setInterval(function() {
			if (ss >= 1) {
				ss = ss - 1;
				$('.Ajax_Out').text(ss);
			} else {
				clearInterval(picInt);
				try{
					$window.parent.location.href = data.returnData;
					$window.location.href = data.returnData;
				}catch(e){ 
					log('timeout!')
				}
			}
		}, 1000);
		return false;
	}
}

app.config(function($httpProvider) {
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});

app.controller('editPCtrl', function($scope, $http,$window,$timeout) {
	$scope.showFlag = true;
	function initData(){
		if(groupIdS!="" && groupIdS!=null && ifmIdS!="" && ifmIdS!=null){
			url = basePath + 'Distribute/findid.do?Id=' + id + '&groupIdS=' + groupIdS + '&ifmIdS=' + ifmIdS
		}else if(groupIdS!="" && groupIdS!=null && ifmIdS==""){
			url = basePath + 'Distribute/findid.do?articleId=' + articleId + '&ifmId=' + ifmId + '&groupIdS=' + groupIdS;
		}else{
			url = basePath + 'Distribute/findid.do?articleId=' + articleId + '&ifmId=' + ifmId;
		}
		$http.get(url).success(function(data) {
			//console.log(data)
			var j = timeout_json(data,$window);
			if(j == false){ 
				return false;
			}
			var datas = data.returnData,
				newDataList = [];
			//对返回的returnData数据进行重新组合成所需的数组数据
			for(var i=0; i<datas.length; i++){
				var key = datas[i];
				if(key.groupName){
					var Gnames = key.groupName.split(','),
						Gids = key.tissueId.split(',');
						
					for(var j = 0; j < Gnames.length; j++){
						newDataList.push({
							ifmId: key.ifmId,
							ifmName: key.ifmName,
							tissueId: Gids[j],
							groupName: Gnames[j]
						});
					}
				}else{
					newDataList.push({
						ifmId: key.ifmId,
						ifmName: key.ifmName,
						tissueId: key.tissueId,
						groupName: key.groupName
					});
				}
			}
//			console.log(newDataList)
			$scope.datas = newDataList;
			if(data.returnData.length>0){
				$scope.showFlag = true;
			}else{
				$scope.showFlag = false;
			}
		});
	}
	initData();
	
	$scope.domAjax = function(e,iId,name) {
		if(e==='del'){
			$.layer({
				title: "温馨提示：",
				shade: [0],
				area: ['230', 'auto'],
				dialog: {
					msg: '确定删除吗？',
					btns: 2,
					type: -1,
					btn: ['确定', '取消'],
					yes: function() {
						//模拟接口数据
						$http.get(basePath + 'Distribute/delete.do?articleId=' + articleId + '&ifmId=' + iId+'&name='+name).success(function(data) {
							var j = timeout_json(data, $window);
							if(j == false) {
								return false;
							}
							layer.msg(data.returnMessage, 1, -1);
							
							$http.get(basePath+"RptStatistics/deleteAndInsterStatistics.do?ifmId="+iId+"&date="+iTime).success(function(data) {});
							
							initData();
						});
					},
					no: function() {}
				}
			});
		}
	}
});
//关闭窗口并刷新列表
function closeCur(){
	window.parent.$(".dtab").find('#jsgo a.cur').trigger('click');
	window.parent.layer.closeAll();
}